package com.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web.common.pojos.Car;
import com.web.common.vo.BrandSaleCounts;
import com.web.common.vo.CarSaleCounts;
import com.web.mapper.CarMapper;
import com.web.service.Task1Service;
import com.web.service.Task2Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/6/8 下午9:43]
 */
@Service
@Slf4j
public class Task2ServiceImpl extends ServiceImpl<CarMapper, Car> implements Task2Service {
    @Autowired
    private CarMapper carMapper;
    @Override
    public CarSaleCounts getCarSaleCounts(int month) {
        CarSaleCounts carSaleCounts = new CarSaleCounts();
        YearMonth yearMonthEnd = YearMonth.of(2024,4);
        YearMonth yearMonthStart = yearMonthEnd.minusMonths(month);
        int start = yearMonthStart.getYear()*100+yearMonthStart.getMonthValue();
        int end = yearMonthEnd.getYear()*100+yearMonthEnd.getMonthValue();
        //2024.4
        List<String> carNameList = carMapper.getCarName(start,end);
        List<Integer> carSaleCountList = new ArrayList<>();
        for (String carName : carNameList) {
            int saleCount = carMapper.getCarCounts(start,end,carName);
            carSaleCountList.add(saleCount);
        }
        log.info("carNameList:{}",carNameList);
        log.info("carSaleCountList:{}",carSaleCountList);
        carSaleCounts.setCarName(carNameList);
        carSaleCounts.setSaleCounts(carSaleCountList);
        return carSaleCounts;
    }

    @Override
    public BrandSaleCounts getBrandSaleCounts(int month) {
        BrandSaleCounts brandSaleCounts = new BrandSaleCounts();
        YearMonth yearMonthEnd = YearMonth.of(2024,4);
        YearMonth yearMonthStart = yearMonthEnd.minusMonths(month);
        int start = yearMonthStart.getYear()*100+yearMonthStart.getMonthValue();
        int end = yearMonthEnd.getYear()*100+yearMonthEnd.getMonthValue();
        List<String> brandNameList =  carMapper.getBrandName(start,end);
        List<Integer> brandSaleCountList = new ArrayList<>();
        for (String brandName : brandNameList) {
            int saleCount = carMapper.getBrandCounts(start,end,brandName);
            brandSaleCountList.add(saleCount);
        }
        log.info("brandNameList:{}",brandNameList);
        log.info("brandSaleCountList:{}",brandSaleCountList);
        brandSaleCounts.setBrandName(brandNameList);
        brandSaleCounts.setSaleCounts(brandSaleCountList);
        return brandSaleCounts;
    }
}
